package org.budo.excel.jdbc.driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.budo.excel.jdbc.driver.parser.jsqlparser.JSqlParserExcelStatementParser;
import org.budo.jdbc.driver.AbstractBudoJdbcDriver;
import org.budo.jdbc.driver.JdbcUrl;
import org.budo.jdbc.driver.parser.SqlStatementParser;
import org.budo.jdbc.driver.parser.cache.CachedJdbcStatementParser;

/**
 * @author lmw
 */
public class BudoExcelJdbcDriver extends AbstractBudoJdbcDriver {
    public static final String[] URL_PREFIX = { "jdbc:excel://" };

    private static final SqlStatementParser DEFAULT_STATEMENT_PARSER = new CachedJdbcStatementParser() //
            .setStatementParser(new JSqlParserExcelStatementParser());

    @Override
    public boolean acceptsURL(String url) throws SQLException {
        return JdbcUrl.acceptsURL(url, URL_PREFIX);
    }

    @SuppressWarnings("resource")
    @Override
    public Connection connect(String url, Properties info) throws SQLException {
        JdbcUrl jdbcUrl = JdbcUrl.parse(url, URL_PREFIX, -1);

        return new BudoExcelJdbcConnection() //
                .statementParser(DEFAULT_STATEMENT_PARSER) // 
                .jdbcUrl(jdbcUrl);
    }
}